<?php

namespace PAT\CartBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * PAT\CartBundle\Entity\Cart
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="PAT\CartBundle\Entity\CartRepository")
 */
class Cart
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    protected $productsList;

    /**
     * Get id
     *
     * @return integer 
     */
    protected function getId()
    {
        return $this->id;
    }
    
    /**
     * Get ProductList
     * 
     * @return array of products and quantity
     */
    public function getProductList()
    {
        return $this->productsList;
    }
    
    
    public function __construct()
    {
    }
    
    /**
     * Set productList
     * 
     * @param type $productList
     */
    public function setProductList($productList)
    {
        $this->productsList = $productList;
    }
    
    /**
     * Add a product to the cart
     * 
     * @param type $product
     */
    public function addToProductList($product)
    {   
        $bool = false;
        
        if($this->productsList != null)
        {
            foreach($this->productsList as $line)
            {

                if( isset($this->productsList[$product->getId()]) )
                {
                    $bool = true;
                }
            }
        }
        
        if($bool == true) 
        {
            $this->productsList[$product->getId()]["product"] =  array("name" => $product->getName(), 
                                                            "priceHT" => $product->getPriceHT(),
                                                            "picture" => $product->getPicture()
                                                            );
            $this->productsList[$product->getId()]["quantity"] += 1;
        } else 
        {
            $productArray =  array("name" => $product->getName(), 
                                                            "priceHT" => $product->getPriceHT(),
                                                            "picture" => $product->getPicture()
                                                            );
            $this->productsList[$product->getId()] = array("product" => $productArray,
                                                           "quantity" => 1);
        }

        
    }
    
    /**
     * 
     * @param type $product
     * @param type $quantity
     */
    public function addToProductListWithQuantity($product, $quantity)
    {
        $array = $this->productsList;
        //var_dump($array);
        $bool = false;
        foreach($this->productsList as $line)
        {
            if( isset($this->productsList[$product->getId()]) )
            {
                $bool = true;
            }
        }
        
        if($bool == true) 
        {
            $this->productsList[$product->getId()]["product"] =  array( "name" => $product->getName(), 
                                                                        "priceHT" => $product->getPriceHT(),
                                                                        "picture" => $product->getPicture()
                                                                 );
            $this->productsList[$product->getId()]["quantity"] += $quantity;
        }
        else 
        {
            $this->productsList[$product->getId()]["product"] =  array( "name" => $product->getName(), 
                                                                        "priceHT" => $product->getPriceHT(),
                                                                        "picture" => $product->getPicture()
                                                                      );
            $this->productsList[$product->getId()]["quantity"] = $quantity;
        }
    }

}
